package com.yunxiao.cloud.sharding.controller;

import com.yunxiao.cloud.common.base.BaseResultResponse;
import com.yunxiao.cloud.common.constant.ExceptionConstant;
import com.yunxiao.cloud.common.util.RedisUtil;
import com.yunxiao.cloud.sharding.entity.QueryParam;
import com.yunxiao.cloud.sharding.entity.User;
import com.yunxiao.cloud.sharding.service.UserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @description: UserController
 * @date: 2019/12/25 13:39
 * @author: yunxiao
 * @version: 1.0
 */
@Api(value = "用户信息", tags = {"用户信息的测试方法"})
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private RedisUtil redisUtil;

    @PostMapping("/info")
    public BaseResultResponse getUserInfo(@RequestParam Integer id) {
        BaseResultResponse response;
        User user = userService.getById(id);
        response = BaseResultResponse.ok(user);
        return response;
    }

    @PostMapping("/RedisTestInfo")
    public BaseResultResponse redisTest(@RequestParam Integer id) {
        BaseResultResponse response;

        redisUtil.set("redisUtil", "8783786723");
        redisTemplate.opsForValue().set("test","12345567734433434343434");
        User user = userService.getById(id);

        redisTemplate.opsForValue().set(user.getUserId(), user);
        response = BaseResultResponse.ok(user);
        return response;
    }

    @PostMapping("/add")
    public BaseResultResponse addUserInfo(@RequestBody User user) {
        BaseResultResponse response;
        user.setCreateTime(new Date());
        boolean result = userService.save(user);
        response = BaseResultResponse.ok(result);
        return response;
    }

    @PostMapping("/queryPage")
    public BaseResultResponse queryPage(@RequestBody QueryParam queryParam, BindingResult bindingResult) {
        BaseResultResponse response;

        if (queryParam.getPageNo() < 1 || queryParam.getPageSize() < 1) {
            return BaseResultResponse.error(ExceptionConstant.ERROR_PAGE_CODE, ExceptionConstant.ERROR_PAGE_MSG);
        }

        response = BaseResultResponse.ok(userService.queryPage(queryParam));
        return response;
    }

    @PostMapping("/queryPageByPageHelper")
    public BaseResultResponse queryPageByPageHelper(@RequestBody QueryParam queryParam) {
        BaseResultResponse response;
        if (queryParam.getPageNo() < 1 || queryParam.getPageSize() < 1) {
            return BaseResultResponse.error(ExceptionConstant.ERROR_PAGE_CODE, ExceptionConstant.ERROR_PAGE_MSG);
        }
        response = BaseResultResponse.ok(userService.pageHelper(queryParam));
        return response;
    }
}
